require("View")

LocationsView = class("LocationsView", View)

---

function LocationsView:initialize()

	View.initialize(self)
	
	self.rootWindow = wx.wxTreeCtrl( 
		gView.frame, 
		wx.wxID_ANY,
        wx.wxDefaultPosition, 
        wx.wxSize(-1, -1),
        wx.wxTR_LINES_AT_ROOT + wx.wxTR_HAS_BUTTONS
    )     
        
	self.rootId = self.rootWindow:AddRoot( "Locations" )
end

---

function LocationsView:update()

	self.rootWindow:DeleteChildren(self.rootId)
	
	for locationName, locationEntity in pairs(gField.entityTypes.Location) do
	
		self:addLocation(locationEntity, self.rootId)
	end
	
	self.rootWindow:Expand(self.rootId)
end

---

function LocationsView:addLocation(locationEntity, parentID)

	-- add Physicals
	
	local typesID = {}
	local locationId = self.rootWindow:AppendItem(parentID, locationEntity.name )
	
	for physicalName, physicalEntity in pairs(gField.entityTypes.Physical) do
	
		local entityInLocationProbability = physicalEntity.CurrentContainer[locationEntity]
		if entityInLocationProbability then

			if not typesID[physicalEntity.type] then
			
				typesID[physicalEntity.type] = self.rootWindow:AppendItem(locationId, physicalEntity.type )
			end
						
			local physicalId = self.rootWindow:AppendItem(
				typesID[physicalEntity.type], 
				physicalName .. "   (" .. entityInLocationProbability .. ")" 
			)
		end
	end
	
	if locationEntity.name == gPlayer.body:getCollapsedProperty("CurrentContainer").name then
		self.rootWindow:SetItemBold(locationId)
	end
	
	for typeName, typeID in pairs(typesID) do
	
		self.rootWindow:Expand(typeID)
	end
	
	self.rootWindow:Expand(locationId)
	
	
	-- add close and locks
	
	self.locksID = false
	
	for doorName, doorEntity in pairs(locationEntity.doorsToNeighborhood) do
	
		self:addDoor(doorEntity, locationId)
	end
	
	for doorName, doorEntity in pairs(locationEntity.doorsToContainedLocations) do
	
		self:addDoor(doorEntity, locationId)
	end
		
	if self.locksID then
	
		self.rootWindow:Expand(self.locksID)
	end
	
	
	--- add contained locations
	
	local subContainersID = false
	
	for containedLocationName, containedLocationEntity in pairs(locationEntity.subContainers) do
	
		if not subContainersID then
		
			subContainersID = self.rootWindow:AppendItem(locationId, "Contained Locations" )
		end
		
		self:addLocation(containedLocationEntity, subContainersID)
	end
		
	if subContainersID then
	
		self.rootWindow:Expand(subContainersID)
	end
end

---

function LocationsView:addDoor(doorEntity, locationId)

	if not doorEntity.noLock then
	
		if not self.locksID then
		
			self.locksID = self.rootWindow:AppendItem(locationId, "Closed and locked doors" )
		end
		
		local doorStateText
		
		if doorEntity:hasUndeterminedSate() then
		
			doorStateText = (
				" = opened (" .. 
				tostring(doorEntity["EnablingSteps"][Door.OpenedStep]) .. 
				") , locked (" .. 
				tostring(doorEntity["EnablingSteps"][Door.LockedStep]) .. 
				")"			
			)
		else
			if doorEntity:isEnabled() then
			
				doorStateText = " = open"
			else
				doorStateText = " = locked"
			end
		end
		
		self.rootWindow:AppendItem(self.locksID, doorEntity.name .. doorStateText)
	end
end